'''
名称：娃娃鱼自动化运维-SSH配置文件
作者：颜涛
时间：2018/5/19
'''
#引入时间模块和SSH模块
import time,paramiko
#SSH连接并执行删除前面备份
def ssh2(ip,username,passwd):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip,22,username,passwd,timeout=5)
    chan=ssh.invoke_shell()
    chan.send('del mac.csv\n')
    time.sleep(1)
    chan.send('y\n')
    time.sleep(5)
    chan.send('dis curr >> mac.csv\n')
    chan.close()
    ssh.close()
#SSH增加MAC绑定
def update(ip,username,passwd,uip,umac):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip,22,username,passwd,timeout=5)
    chan=ssh.invoke_shell()
    chan.send('sys\n')
    time.sleep(1)
    chan.send('user-bind static ip-address ' + uip + ' mac-address ' + umac + '\n')
    time.sleep(2)
    chan.send('ip pool vlanc\n')
    time.sleep(1)
    chan.send('static-bind ip-address ' + uip + ' mac-address ' + umac + '\n')
    time.sleep(2)
    chan.send('commit\n')
    time.sleep(2)
    #output=chan.recv(2024)
    #print(output)
    chan.close()
    ssh.close()
#SSH删除MAC绑定
def delete(ip,username,passwd,uip,umac):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip,22,username,passwd,timeout=5)
    chan=ssh.invoke_shell()
    chan.send('sys\n')
    time.sleep(1)
    chan.send('undo user-bind static ip-address ' + uip + ' mac-address ' + umac + '\n')
    time.sleep(2)
    chan.send('ip pool vlanc\n')
    time.sleep(1)
    chan.send('undo static-bind ip-address ' + uip + '\n')
    time.sleep(2)
    chan.send('commit\n')
    chan.close()
    ssh.close()
